/*
*

	@author: taco
	@Date: 2023/1/28
	@Time: 13:55

*
*/
package utils_jwt

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"log"
	"sort"
	"strings"
)

func CreateSign(token, timestamp, key string) string {
	signMap := make(map[string]string)
	signMap["token"] = token
	signMap["timestamp"] = timestamp
	signStr := Ksort(signMap)
	signStr += fmt.Sprintf("&key=%s", key)
	log.Printf("打印一下签名: %s", signStr)
	m := md5.New()
	m.Write([]byte(signStr))
	bytes := m.Sum(nil)
	sign := strings.ToUpper(hex.EncodeToString(bytes))
	return sign
}
func Ksort(paramsMap map[string]string) string {
	var slice []string
	for key, _ := range paramsMap {
		slice = append(slice, key)
	}
	sort.Strings(slice)

	var str string
	for num, key := range slice {
		if len(slice)-1 != num {
			str += fmt.Sprintf("%s=%s", key, paramsMap[key])
			str += "&"
		} else {
			str += fmt.Sprintf("%s=%s", key, paramsMap[key])
		}
	}

	return str
}
